package com.laundry.controller;

import com.laundry.dto.OrderDTO;
import com.laundry.dto.ResultDTO;
import com.laundry.entity.Order;
import com.laundry.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/api/order")
public class OrderController {
    
    @Autowired
    private OrderService orderService;
    
    @PostMapping("/create")
    public ResultDTO createOrder(@RequestBody OrderDTO orderDTO) {
        return orderService.createOrder(orderDTO);
    }
    
    @GetMapping("/list")
    public ResultDTO getOrderList(@RequestParam(required = false) Long userId, 
                                 @RequestParam(required = false) String status) {
        try {
            if (userId == null) {
                // 管理员查询所有订单
                return orderService.getAdminOrderList();
            } else {
                // 用户查询自己的订单
                if (status == null || status.isEmpty()) {
                    return orderService.getOrderList(userId, null);
                } else if (status.contains(",")) {
                    // 多状态查询交给前端处理，因为我们的getOrderList方法
                    // 已经返回了包含关联信息的完整订单数据
                    return orderService.getOrderList(userId, null);
                } else {
                    // 单状态查询
                    try {
                        Integer statusInt = Integer.parseInt(status);
                        return orderService.getOrderList(userId, statusInt);
                    } catch (NumberFormatException e) {
                        return ResultDTO.error("无效的状态参数");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("获取订单列表失败");
        }
    }
    
    /**
     * 将逗号分隔的状态字符串解析为Integer列表
     */
    private List<Integer> parseStatusParam(String status) {
        if (status == null || status.isEmpty()) {
            return Collections.emptyList();
        }
        
        List<Integer> statusList = new ArrayList<>();
        String[] statusArray = status.split(",");
        
        for (String s : statusArray) {
            try {
                statusList.add(Integer.parseInt(s.trim()));
            } catch (NumberFormatException e) {
                // 忽略无效的状态值
            }
        }
        
        return statusList;
    }
    
    @GetMapping("/detail")
    public ResultDTO getOrderDetail(@RequestParam String orderNo) {
        return orderService.getOrderDetail(orderNo);
    }
    
    @PutMapping("/cancel")
    public ResultDTO cancelOrder(@RequestParam String orderNo) {
        return orderService.cancelOrder(orderNo);
    }
    
    @PutMapping("/pay")
    public ResultDTO payOrder(@RequestParam String orderNo, @RequestParam String paymentMethod) {
        return orderService.payOrder(orderNo, paymentMethod);
    }
} 